package com.guguguweb.conn;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class NettyClient {
    private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);


    private Bootstrap b =  new Bootstrap();
    EventLoopGroup group = new NioEventLoopGroup();




    private void initBootstrap(){
        b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new NettyMessageDecoder(1024 * 1024, 4, 4, -8, 0));
                        p.addLast(new NettyMessageEncoder());
                        p.addLast(new LoginAuthReqHandler());
                        p.addLast(new HeartBeatReqHandler());
                    }
                });
    }


    public Channel connectSyc(int port, String host){
        System.out.println("发起同步连接");
        ChannelFuture connect = b.connect(
                new InetSocketAddress(host, port));
        Channel channel = connect.channel();
        return channel;
    }



    public void connect(int port, String host) {
        try {
            //发起异步连接
            System.out.println("发起异步连接");
            ChannelFuture future = b.connect(
                    new InetSocketAddress(host, port)).sync();
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            //所有资源释放完成，清空资源，再次发起重连
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("sleep");
                        TimeUnit.SECONDS.sleep(5);
                        try {
                            System.out.println("再次发起重连");
                            connect(NettyConstant.PORT, NettyConstant.REMOTE_IP);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    public static void main(String[] args) {
        new NettyClient().connect(NettyConstant.PORT, NettyConstant.REMOTE_IP);
    }
}
